package com.llt.bottomnavigationf.util;

import com.llt.bottomnavigationf.OnNavClickListener;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;

import java.lang.reflect.Method;
import java.util.Map;

public class RouterUtil {
    /**
     *
     * @param abilitySlice 当前调用页面的 abilitySlice 如果是fractionPage 用导入的abilitySlice.getAbility();
     * @param classTo
     * @param map 请求参数
     * @throws Exception
     */
    public static void navigate(AbilitySlice abilitySlice , Class classTo,Map<String,Object> map) throws Exception {
        String abilityName = classTo.getName();//获取类名
        String bundleName = classTo.getPackage().getName();//获取包名
        Intent intent = new Intent();
        Operation operationBuilder = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName (bundleName)//包名字
                .withAbilityName(abilityName)
                .build()
                ;
        intent.setOperation(operationBuilder);
        IntentParams intentParams = new IntentParams();
        map.forEach((key,value)->{
            intentParams.setParam(key,value);
        });
        intent.setParams(intentParams);
        abilitySlice.startAbility(intent);
    }
    /**
     *
     * @param abilitySlice 当前调用页面的 abilitySlice 如果是fractionPage 用导入的abilitySlice.getAbility();
     * @param classTo
     * @throws Exception
     */
    public static void navigate(AbilitySlice abilitySlice , Class classTo) throws Exception {
        String abilityName = classTo.getName();//获取类名
        String bundleName = classTo.getPackage().getName();//获取包名
        Intent intent = new Intent();
        Operation operationBuilder = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName (bundleName)//包名字
                .withAbilityName(abilityName)
                .build()
                ;
        intent.setOperation(operationBuilder);
        abilitySlice.startAbility(intent);
    }
    /**
     *
     * @param abilitySlice 当前调用页面的 abilitySlice 如果是fractionPage 用导入的abilitySlice.getAbility();
     * @param abilitySliceTo
     * @param map 请求参数
     * @throws Exception
     */
    public static void navigate(AbilitySlice abilitySlice , AbilitySlice abilitySliceTo,Map<String,Object> map) throws Exception {

        Intent intent = new Intent();
        IntentParams intentParams = new IntentParams();
        map.forEach((key,value)->{
            intentParams.setParam(key,value);
        });
        intent.setParams(intentParams);
        abilitySlice.present(abilitySliceTo,intent);
    }

    /**
     *
     * @param abilitySlice 当前调用页面的 abilitySlice 如果是fractionPage 用导入的abilitySlice.getAbility();
     * @param abilitySliceTo
     * @throws Exception
     */
    public static void navigate(AbilitySlice abilitySlice , AbilitySlice abilitySliceTo) throws Exception {
        Intent intent = new Intent();
        abilitySlice.present(abilitySliceTo,intent);

    }
    private static String navigateToType(Class classname){
        String name="";
        Method[] methods = classname.getMethods();
        for (Method method : methods) {
            if("getAbility"==method.getName()){
                name = method.getName();
                break;
            }
            if("getAbilityName"==method.getName()){
                name = method.getName();
                break;
            }
            if("getFractionAbility"==method.getName()){
                name = method.getName();
                break;
            }

        }
        return name;
    }
}
